//
// Created by Jisam on 2024/8/13 23:26.
// solve c
#include <bits/stdc++.h>

#define endl "\n"
#define PSI pair<string,int>
#define PII pair<int,int>
#define PDI pair<double,int>
#define PDD pair<double,double>
#define VVI vector<vector<int>>
#define VI vector<int>
#define VS vector<string>

#define PQLI priority_queue<int, vector<int>, less<int>>
#define PQGI priority_queue<int, vector<int>, greater<int>>
#define u32 unsigned
#define i64 long long
#define u64 unsigned long long

using namespace std;


void solve() {
    int n;
    cin >> n;
    map<int, vector<int>> indexMap;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        indexMap[a[i]].push_back(i);
    }
    int m;
    cin >> m;
    for (int i = 0; i < m; i++) {
        string s;
        cin >> s;
        map<char, int> mmp;
        int ok = 1;
        if(s.length() != a.size()) ok = 0;
        if (ok) {

            for (int i = 0; i < n; i++) {
                mmp[s[i]] = a[i];
            }
            for (int i = 0; i < n; i++) {
                if (mmp[s[i]] != a[i]) ok = 0;
            }
        }

        if (ok) {
            for (auto &[x, v]: indexMap) {
//                for (int k = 0; k < v.size(); k++) {
//                    cout << v[k] << " ";
//                }
//                cout << endl;
                if (v.size() <= 1) continue;

                for (int k = 1; k < v.size(); k++) {
                    if (s[v[k - 1]] != s[v[k]]) {
                        ok = 0;
                        break;
                    }
                }
                if (ok == 0) break;
            }
        }
        if (ok) {
            cout << "YES" << endl;
        } else cout << "NO" << endl;
    }

}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T = 1;
    cin >> T;
    while (T--) solve();
//    cout << "\n"[];
    return 0;
}